package com.example.shiyansan.controller.user;


import com.example.shiyansan.dto.UserLoginDTO;
import com.example.shiyansan.entity.User;
import com.example.shiyansan.result.Result;
import com.example.shiyansan.service.UserService;
import com.example.shiyansan.vo.UserLoginVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user/user")
public class UserController {

    @Autowired
    private UserService userService;
    /**
     * 用户登录
     */
    @PostMapping("/login")
    public Result<UserLoginVo> login(@RequestBody @Validated UserLoginDTO userLoginDTO) throws Exception {
        UserLoginVo userLoginVo = userService.findByUsernameAndPassword(userLoginDTO);
        return Result.success(userLoginVo);
    }

    /**
     * 用户注册
     */
    @PostMapping("/register")
    public Result register(@RequestBody @Validated User user) throws Exception {
        userService.register(user);
        return Result.success();
    }
}
